<template>
  <div class="login-log">
    <!-- 搜索和筛选 -->
    <el-card class="filter-card">
      <el-form :model="searchForm" class="filter-form" :inline="true">
        <el-form-item label="用户名/用户ID">
          <el-input
            v-model="search.keyword"
            placeholder="请输入用户名或用户ID"
            prefix-icon="el-icon-search"
            style="width: 200px;"
          ></el-input>
        </el-form-item>
        
        <el-form-item label="开始日期">
          <el-date-picker
            v-model="search.startDate"
            type="date"
            placeholder="选择开始日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :picker-options="datePickerOptions"
            style="width: 150px;"
          ></el-date-picker>
        </el-form-item>
        
        <el-form-item label="结束日期">
          <el-date-picker
            v-model="search.endDate"
            type="date"
            placeholder="选择结束日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :picker-options="datePickerOptions"
            style="width: 150px;"
          ></el-date-picker>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="fetchData" icon="el-icon-search">
            查询
          </el-button>
          <el-button @click="handleReset" icon="el-icon-refresh">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-card">
      <el-table
        :data="logs"
        v-loading="loading"
        class="login-table"
        stripe
        border
      >
        <el-table-column
          prop="UserName"
          label="用户名"
          width="150"
        ></el-table-column>
        
        <el-table-column
          prop="UserId"
          label="用户ID"
          width="150"
        ></el-table-column>
        
        <el-table-column
          prop="IP"
          label="IP地址"
          width="150"
        ></el-table-column>
        
        <el-table-column
          label="身份"
          width="120"
        >
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.role === '0' ? 'success' : 'primary'"
              size="small"
            >
              {{ scope.row.role === '0' ? '专家' : '投标人' }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column
          prop="CreateTime"
          label="登录时间"
          width="180"
        >
          <template slot-scope="scope">
            {{ formatDate(scope.row.CreateTime) }}
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-wrapper">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="itemsPerPage"
          :total="totalItems"
          layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { GetLoginLogPage } from '@/api/LoginLog'

export default {
  name: 'LoginLog',
  data() {
    return {
      loading: false,
      // 分页
      page: 1,
      itemsPerPage: 10,
      totalItems: 0,
      // 查询条件
      search: {
        keyword: '',
        startDate: '',
        endDate: ''
      },
      searchForm: {},
      // 日期选择器配置
      datePickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      logs: []
    }
  },
  mounted() {
    const today = this.getToday()
    this.search.startDate = today.substring(0, 7) + '-01'
    this.search.endDate = today
    this.fetchData()
  },
  methods: {
    // 获取今天 yyyy-MM-dd
    getToday() {
      const d = new Date()
      const yyyy = d.getFullYear()
      const mm = String(d.getMonth() + 1).padStart(2, '0')
      const dd = String(d.getDate()).padStart(2, '0')
      return `${yyyy}-${mm}-${dd}`
    },
    async fetchData() {
      this.loading = true
      try {
        const params = {
          pageIndex: this.page,
          pageSize: this.itemsPerPage,
          key: this.search.keyword,
          start: this.search.startDate,
          end: this.search.endDate
        }
        const res = await GetLoginLogPage(params)
        // 数据结构：res.Data.Data 为列表，res.Data.Count 为总数（与其他页面保持一致）
        this.logs = (res && res.Data && res.Data.Data) ? res.Data.Data : []
        this.totalItems = (res && res.Data && res.Data.Count) ? res.Data.Count : 0
      } catch (error) {
        console.error('获取登录记录失败:', error)
        this.$message.error('获取登录记录失败')
      } finally {
        this.loading = false
      }
    },
    
    // 格式化日期
    formatDate(date) {
      return new Date(date).toLocaleString()
    },
    
    // 重置搜索条件
    handleReset() {
      const today = this.getToday()
      this.search = {
        keyword: '',
        startDate: today.substring(0, 7) + '-01',
        endDate: today
      }
      this.page = 1
      this.fetchData()
    },
    
    // 分页大小改变
    handleSizeChange(val) {
      this.itemsPerPage = val
      this.page = 1
      this.fetchData()
    },
    
    // 当前页改变
    handleCurrentChange(val) {
      this.page = val
      this.fetchData()
    }
  }
}
</script>

<style scoped>
.login-log {
  padding: 24px 0;
}

.filter-card {
  margin-bottom: 20px;
}

.filter-form {
  padding: 20px;
}

.filter-actions {
  text-align: right;
  margin-top: 20px;
}

.table-card {
  margin-top: 20px;
}

.login-table {
  width: 100%;
}

.pagination-wrapper {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  padding: 20px;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .filter-form .el-form-item {
    margin-bottom: 15px;
  }
  
  .filter-actions {
    text-align: center;
  }
  
  .pagination-wrapper {
    padding: 10px;
  }
  
  .login-table {
    font-size: 12px;
  }
}
</style>